package com.face.server.dao;

import com.face.server.entity.Apply;
import com.face.server.entity.Student;
import com.face.server.service.UtileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
 * Created by ryan on 2018/3/27.
 */
@Repository
public class ApplyDao {
    @Autowired
    MongoTemplate mongoTemplate;

    public List<Apply> list(Pageable pageable, Date begin, Date end, String input) {
        Criteria criteria=new Criteria();

        Criteria criState=new Criteria();
        if (input.equals("")==false&&UtileService.isNum(input)){
            criState.and("status").is(Integer.parseInt(input));
        }

        List<Student> students=mongoTemplate.find(Query.query(
                where("type").is(0).and("name").regex(input)
        ),Student.class,"joined");
        List<String> idlist=new ArrayList<String>();
        if (students!=null&&students.size()>0){
            students.stream().forEach(e->
                    idlist.add(e.getId())
            );
        }
        Criteria cristu=new Criteria();
        if (idlist!=null&&idlist.size()>0)
            cristu.and("student.$id").in(idlist);

        Criteria criTime=new Criteria();
        criTime.and("effectDate").gte(begin).and("finishDate").lte(end);

        Aggregation aggregation=Aggregation.newAggregation(
                Aggregation.match(new Criteria().andOperator(new Criteria().orOperator(criteria,cristu),criTime)),
                Aggregation.sort(pageable.getSort()),
                Aggregation.skip((long)pageable.getOffset()),
                Aggregation.limit(pageable.getPageSize())
        );

        return mongoTemplate.aggregate(aggregation,"apply",Apply.class).getMappedResults();

    }

    public List<Apply> listMy(Pageable pageable, Date begin, Date end, String input, List<Integer> dormitoryNumList) {
        Criteria criteria=new Criteria();

        Criteria criState=new Criteria();
        if (input.equals("")==false&&UtileService.isNum(input)){
            criState.and("status").is(Integer.parseInt(input));
        }

        List<Student> students=mongoTemplate.find(Query.query(
                where("type").is(0).and("name").regex(input).and("dormitoryBuildNum").in(dormitoryNumList)
        ),Student.class,"joined");
        List<String> idlist=new ArrayList<String>();
        if (students!=null&&students.size()>0){
            students.stream().forEach(e->
                    idlist.add(e.getId())
            );
        }
        Criteria cristu=new Criteria();
        if (idlist!=null&&idlist.size()>0)
            cristu.and("student.$id").in(idlist);

        Criteria criTime=new Criteria();
        criTime.and("effectDate").gte(begin).and("finishDate").lte(end);

        Aggregation aggregation=Aggregation.newAggregation(
                Aggregation.match(new Criteria().andOperator(new Criteria().orOperator(criteria,cristu),criTime)),
                Aggregation.sort(pageable.getSort()),
                Aggregation.skip((long)pageable.getOffset()),
                Aggregation.limit(pageable.getPageSize())
        );

        return mongoTemplate.aggregate(aggregation,"apply",Apply.class).getMappedResults();
    }
}
